#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/11/27 17:41
# @Author  : leon yan
# @Site    : 
# @File    : alarmProcess.py
# @Software: PyCharm
import datetime

import Globalist as gl


def rdAlarm():
    aa = []
    CurrentTime = datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S')
    bAlarm = False
    for i in range(10):
        if i == 0:
            atPosition = 'A'
        else:
            atPosition = 'B' + str(i)
        # 1
        if gl.get_value("xEmergencyStop1_" + str(i)):
            bAlarm = True
            if not gl.get_value("bAlmRecorded1_" + str(i)):
                aa.append([CurrentTime, 'Emergency stop button #1 pressed', atPosition, '1000'])
                gl.set_value("bAlmRecorded1_" + str(i), True)
        else:
            gl.set_value("bAlmRecorded1_" + str(i), False)
        # 2
        if gl.get_value("xEmergencyStop2_" + str(i)):
            bAlarm = True
            if not gl.get_value("bAlmRecorded2_" + str(i)):
                aa.append([CurrentTime, 'Emergency stop button #2 pressed', atPosition, '1000'])
                gl.set_value("bAlmRecorded2_" + str(i), True)
        else:
            gl.set_value("bAlmRecorded2_" + str(i), False)
        # 3
        if gl.get_value("xUpRollerAlarm_" + str(i)):
            bAlarm = True
            if not gl.get_value("bAlmRecorded3_" + str(i)):
                aa.append([CurrentTime, 'Upper electric roller failure', atPosition, '1001'])
                gl.set_value("bAlmRecorded3_" + str(i), True)
        else:
            gl.set_value("bAlmRecorded3_" + str(i), False)
        # 4
        if gl.get_value("xDownRollerAlarm_" + str(i)):
            bAlarm = True
            if not gl.get_value("bAlmRecorded4_" + str(i)):
                aa.append([CurrentTime, 'Lower electric roller failure', atPosition, '1001'])
                gl.set_value("bAlmRecorded4_" + str(i), True)
        else:
            gl.set_value("bAlmRecorded4_" + str(i), False)

        # 5
        if gl.get_value("xSteppingAlarm_" + str(i)):
            bAlarm = True
            if not gl.get_value("bAlmRecorded5_" + str(i)):
                aa.append([CurrentTime, 'Stepping motor failure', atPosition, '1002'])
                gl.set_value("bAlmRecorded5_" + str(i), True)
        else:
            gl.set_value("bAlmRecorded5_" + str(i), False)
    # 6
    if gl.get_value("xServo1Alarm"):
        bAlarm = True
        if not gl.get_value("bAlmRecorded6"):
            aa.append([CurrentTime, 'Servo #1 failure', 'Conveyor', '1003'])
            gl.set_value("bAlmRecorded6", True)
    else:
        gl.set_value("bAlmRecorded6", False)
    # 7
    if gl.get_value("xServo2Alarm"):
        bAlarm = True
        if not gl.get_value("bAlmRecorded7"):
            aa.append([CurrentTime, 'Servo #2 failure', 'Conveyor', '1004'])
            gl.set_value("bAlmRecorded7", True)
    else:
        gl.set_value("bAlmRecorded7", False)
    # 8
    if gl.get_value("xFrequencyAlarm"):
        bAlarm = True
        if not gl.get_value("bAlmRecorded8"):
            aa.append([CurrentTime, 'Frequency inverter failure', 'Conveyor', '1005'])
            gl.set_value("bAlmRecorded8", True)
    else:
        gl.set_value("bAlmRecorded8", False)
    # 9
    if gl.get_value("xEmergencyStop3_0"):
        bAlarm = True
        if not gl.get_value("bAlmRecorded9"):
            aa.append([CurrentTime, 'Emergency stop button pressed', 'Panel', '1000'])
            gl.set_value("bAlmRecorded9", True)
    else:
        gl.set_value("bAlmRecorded9", False)
    # 10
    if gl.get_value("xEmergencyStop4_0"):
        bAlarm = True
        if not gl.get_value("bAlmRecorded10"):
            aa.append([CurrentTime, 'Emergency stop button pressed', 'Conveyor', '1000'])
            gl.set_value("bAlmRecorded10", True)
    else:
        gl.set_value("bAlmRecorded10", False)

    if aa:
        return aa, bAlarm
    else:
        return None


def systemCheck():
    ready_bit_Dict = {}
    alarm_bit_Dict = {}
    for i in range(10):
        ready_bit_Dict[i] = gl.get_value("xReady_" + str(i))
        alarm_bit_Dict[i] = gl.get_value("xAlarm_" + str(i))
    return ready_bit_Dict, alarm_bit_Dict